<?php 
/*
 module:		首页
 create_time:	2021-09-18 22:56:53
 author:		
 contact:		
*/

namespace app\api\controller;


use app\api\model\Index as IndexModel;
use EasyWeChat\MiniProgram\Auth\Client;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;

    class Login1 extends Common {
        /**start**/
        public function login()
        {
            $code = $this->request->post('code');
            $pid = $this->request->post('pid');
            if(empty($code)){
              return  $this->ajaxError('微信未授权');
            }
            $info = $this->request->post('info');
            $info = htmlspecialchars_decode($info);
            $infoarr = json_decode($info,true);
            $wxcode = config('my')['mini_program'];
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$wxcode['app_id']}&secret={$wxcode['secret']}&js_code={$code}&grant_type=authorization_code";
            $res = file_get_contents($url);
            $openid = json_decode($res,true);

            $data = [];
            $data['nick_name'] = $infoarr['nickName'];
            $data['create_time'] =time();
            $data['avatar_url'] = $infoarr['avatarUrl'];
            $data['gender'] = $infoarr['gender'];
            $data['city'] = $infoarr['city'];
            $data['province'] = $infoarr['province'];
            $data['openid'] = $openid['openid'];
            $post = $this->request->post();
            $results = print_r($post,true);
            $myfile = fopen("ces.txt", "a+") or die("Unable to open file!");
            fwrite($myfile, $results);
            fclose($myfile);

            $r = Db::name('users')->where('openid',$openid['openid'])->find();
            if(empty($r)){
                $act_code = $this->act_code();
                $data['share'] = $act_code;
                $data['pid'] = Db::name('users')->where('share',$pid)->value('u_id');

                $uid = Db::name('users')->insertGetId($data);
            }else{
                $act_code = $r['share'];
                if(empty($r['pid'])){
                    $data['pid'] = Db::name('users')->where('share',$pid)->value('u_id');
                }
                $uid = $r['u_id'];
                Db::name('users')->where('u_id',$uid)->update($data);
            }
            $res = [];
            $token = $this->setToken($uid);

            $res['token'] =$token;
            $res['share'] = $act_code;
           $this->ajaxSuccess($res);
        }

        public function act_code()
        {
            while (true) {
                $code =  random(8, 'all');
                $count = Db::name('users')->where('share',$code)->count();
                if (empty($count)) {
                    $ac_code = $code;
                    return $ac_code;
                }
            }
        }

        public function ces()
        {
            $arr =
                ['nick_name' => 'Horse🧐',
        'create_time' => '1632587496',
        'avatar_url' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eokYico9TRXpn8Dp34soRTqImuibNYgtB2Ymthg99mrz77btibuYKYXxbIuwqdwicG4jTNUkzB7U5sJRw/132',
        'gender' => '1',
        'city' => 'Ankang',
        'province' => 'Shaanxi',
        'openid' => 'oviWm4szTGI8uqmFVgz98eulxUbA'];
            $r = Db::name('users')->insert($arr);
            dump($r);;
        }






    }

